﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
public partial class backstage_navigation_category_set : System.Web.UI.Page
{
    // Fields

    protected CategoryManager cm = new CategoryManager();

    protected int menuCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["MenuCategoryMaxCount"]);
    protected int moduleCategoryMaxCount = 0;
    protected NavigationCategoryManager ncm = new NavigationCategoryManager();
    protected int newsCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["NewsCategoryMaxCount"]);
    protected int promotionCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["PromotionCategoryMaxCount"]);

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ArrayList list;
            DataTable navigationCategoryByNavigationTypeAndLanguageVersion;
            ArrayList list2;
            NavigationCategory category;
            if (base.Request.Form["menuCategory"] != null)
            {
                list = ArrayList.Adapter(base.Request.Form["menuCategory"].Split(new char[] { ',' }));
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(0, this.Session["language_version"].ToString());
                list2 = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    list2.Add(Convert.ToInt32(row["category"]));
                    if (!list.Contains(row["category"].ToString()))
                    {
                        this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                    }
                }
                foreach (string str in list)
                {
                    category = new NavigationCategory();
                    category.Category = Convert.ToInt32(str);
                    category.NavigationType = 0;
                    category.LanguageVersion = this.Session["language_version"].ToString();
                    if (!list2.Contains(category.Category))
                    {
                        this.ncm.AddNavigationCategory(category);
                    }
                }
            }
            else
            {
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(0, this.Session["language_version"].ToString());
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                }
            }
            if (base.Request.Form["newsCategory"] != null)
            {
                list = ArrayList.Adapter(base.Request.Form["newsCategory"].Split(new char[] { ',' }));
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(1, this.Session["language_version"].ToString());
                list2 = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    list2.Add(Convert.ToInt32(row["category"]));
                    if (!list.Contains(row["category"].ToString()))
                    {
                        this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                    }
                }
                foreach (string str in list)
                {
                    category = new NavigationCategory();
                    category.Category = Convert.ToInt32(str);
                    category.NavigationType = 1;
                    category.LanguageVersion = this.Session["language_version"].ToString();
                    if (!list2.Contains(category.Category))
                    {
                        this.ncm.AddNavigationCategory(category);
                    }
                }
            }
            else
            {
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(1, this.Session["language_version"].ToString());
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                }
            }
            if (base.Request.Form["promotionCategory"] != null)
            {
                list = ArrayList.Adapter(base.Request.Form["promotionCategory"].Split(new char[] { ',' }));
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(2, this.Session["language_version"].ToString());
                list2 = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    list2.Add(Convert.ToInt32(row["category"]));
                    if (!list.Contains(row["category"].ToString()))
                    {
                        this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                    }
                }
                foreach (string str in list)
                {
                    category = new NavigationCategory();
                    category.Category = Convert.ToInt32(str);
                    category.NavigationType = 2;
                    category.LanguageVersion = this.Session["language_version"].ToString();
                    if (!list2.Contains(category.Category))
                    {
                        this.ncm.AddNavigationCategory(category);
                    }
                }
            }
            else
            {
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(2, this.Session["language_version"].ToString());
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                }
            }
            if (base.Request.Form["moduleCategory"] != null)
            {
                list = ArrayList.Adapter(base.Request.Form["moduleCategory"].Split(new char[] { ',' }));
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(3, this.Session["language_version"].ToString());
                list2 = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    list2.Add(Convert.ToInt32(row["category"]));
                    if (!list.Contains(row["category"].ToString()))
                    {
                        this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                    }
                }
                foreach (string str in list)
                {
                    category = new NavigationCategory();
                    category.Category = Convert.ToInt32(str);
                    category.NavigationType = 3;
                    category.LanguageVersion = this.Session["language_version"].ToString();
                    if (!list2.Contains(category.Category))
                    {
                        this.ncm.AddNavigationCategory(category);
                    }
                }
            }
            else
            {
                navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(3, this.Session["language_version"].ToString());
                foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
                {
                    this.ncm.DeleteNavigationCategory(Convert.ToInt32(row["id"]));
                }
            }
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"navigation_category_set.aspx\";</script>");
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");location.href=\"navigation_category_set.aspx\";</script>");
        }
    }

    protected StringBuilder MenuCategoryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow("00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString(), -1);
            DataTable navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(0, this.Session["language_version"].ToString());
            ArrayList list = new ArrayList();
            foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
            {
                list.Add(Convert.ToInt32(row["category"]));
            }
            foreach (DataRow row in table.Rows)
            {
                if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"menuCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                }
                else
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"menuCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                }
            }
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder ModuleCategoryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow("00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString(), -1);
            DataTable navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(3, this.Session["language_version"].ToString());
            ArrayList list = new ArrayList();
            foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
            {
                list.Add(Convert.ToInt32(row["category"]));
            }
            foreach (DataRow row in table.Rows)
            {
                if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"moduleCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                }
                else
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"moduleCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                }
            }
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder NewsCategoryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow("00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString(), -1);
            DataTable navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(1, this.Session["language_version"].ToString());
            ArrayList list = new ArrayList();
            foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
            {
                list.Add(Convert.ToInt32(row["category"]));
            }
            foreach (DataRow row in table.Rows)
            {
                if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"newsCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                }
                else
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"newsCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                }
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack)
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath("~/cn/xml/home.xml"));
            this.moduleCategoryMaxCount = Convert.ToInt32(((XmlElement)document.SelectSingleNode("home_ad/category_ad")).GetAttribute("count"));
        }
    }

    protected StringBuilder PromotionCategoryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow("00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString(), -1);
            DataTable navigationCategoryByNavigationTypeAndLanguageVersion = this.ncm.GetNavigationCategoryByNavigationTypeAndLanguageVersion(2, this.Session["language_version"].ToString());
            ArrayList list = new ArrayList();
            foreach (DataRow row in navigationCategoryByNavigationTypeAndLanguageVersion.Rows)
            {
                list.Add(Convert.ToInt32(row["category"]));
            }
            foreach (DataRow row in table.Rows)
            {
                if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"promotionCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                }
                else
                {
                    builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"promotionCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                }
            }
        }
        catch
        {
        }
        return builder;
    }

}